package service.impl;

import dao.AdminDao;
import domain.Admin;
import service.AdminService;
import service.RegisterService;
import util.CheckUtil;
import util.MyBatisUtil;

import java.util.List;

public final class AdminServiceImpl implements AdminService, RegisterService {
    private AdminServiceImpl() {
    }
    private static final AdminServiceImpl INSTANCE = new AdminServiceImpl();
    private static final AdminDao adminDao = MyBatisUtil.getMapper(AdminDao.class);
    public static AdminServiceImpl getINSTANCE() { return INSTANCE; }

    @Override
    public int addAdmin(Admin admin) {
        return adminDao.insert(admin);
    }

    @Override
    public Admin login(String tel, String password) {
        Admin admin = adminDao.selectByTel(tel);
        if (admin!=null && password.equals(admin.getPassword())) return admin;
        return null;
    }

    @Override
    public int verifyRegisterTel(String tel) {
        if (!CheckUtil.checkTel(tel)) {
            // System.out.println(1);
            return 1;
        }
        if (adminDao.selectByTel(tel) != null) {
            return 2;
        }
        return 0;
    }

    @Override
    public int verifyRegisterName(String name) {
        return 0;
    }

    @Override
    public List<Admin> selectAll() {
        return adminDao.selectAll();
    }
}
